package com.yijiu.trb;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.yijiu.algorithm.RSACoder;
import com.yijiu.network.Const;
import com.yijiu.network.NetManager;
import com.yijiu.network.NetObserver;
import com.yijiu.network.Request;
import com.yijiu.resource.C;
import com.yijiu.view.MyProgressDialog;
import com.zvezda.algorithm.utils.AES;

/* 没有标题的Activity */
public class BNotitleActivity extends Activity implements NetObserver
{
	/**
	 * 无网络提示窗口
	 */
	public AlertDialog dialog;

	// /**
	// * 设置按钮
	// */
	// private TextView settingBtn;
	//
	// /**
	// * 退出按钮
	// */
	// private TextView exitBtn;

	/**
	 * 标题栏姓名
	 */
	public TextView realName;

	/**
	 * 标题栏余额
	 */
	public TextView balance;

	protected LayoutInflater inflater;

	protected App app;

	protected NetManager netManager;

	private MyProgressDialog myProgressDialog;

	public String TAG;

	/**
	 * 用公钥加密后的私钥
	 */
	public String encodedData;

	/**
	 * AES加密后的数据
	 */
	public String encriptData;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		TAG = this.getClass().getSimpleName();
		app = (App) this.getApplication();
		netManager = NetManager.getManager();
		inflater = LayoutInflater.from(this);
		myProgressDialog = new MyProgressDialog(this);
		myProgressDialog.setMyCancelable(true);
		myProgressDialog.setMyTouchOutside(false);
		/*------------------------------------------*/
		dialog = new AlertDialog.Builder(this).setTitle("请设置网络").setPositiveButton("设置", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
				startActivity(intent);
			}
		}).setNegativeButton("退出", new DialogInterface.OnClickListener()
		{
			@Override
			public void onClick(DialogInterface dialog, int which)
			{
				System.exit(0);
			}
		}).create();
		dialog.setCanceledOnTouchOutside(false);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
	}

	@Override
	protected void onResume()
	{
		super.onResume();
	}

	@Override
	protected void onPause()
	{
		super.onPause();
	}

	@Override
	protected void onDestroy()
	{
		super.onDestroy();
	}

	public void showProgress(String message)
	{
		myProgressDialog.setMyMessage(message);
		myProgressDialog.myShow();
	}

	public void showProgress(int stringid)
	{
		myProgressDialog.setMyMessage(getString(stringid));
		myProgressDialog.myShow();
	}

	public void showDefaultProgress()
	{
		myProgressDialog.setMyMessage(getString(R.string.default_process_string));
		myProgressDialog.myShow();
	}

	public void setMessage(String message)
	{
		myProgressDialog.setMyMessage(message);
	}

	public void dismissProgress()
	{
		myProgressDialog.mydismiss();
	}

	public boolean isShow()
	{
		if (myProgressDialog.isShowing())
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	@Override
	public void getResult(Request r)
	{
		if (isShow())
		{
			dismissProgress();
		}
	}

	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		if (isShow())
		{
			dismissProgress();
		}
		switch (errorCode)
		{
			case Const.NO_NETWORK:
				if (!dialog.isShowing())
				{
					dialog.show();
					return;
				}
				break;
		}
	}

	@Override
	public void rePost(Request r)
	{
	}

	/** 初始化控件 */
	protected void initWidget()
	{
	};

	/** 不传递数据的页面跳转 */
	protected void initIntent(Context context, Class<?> cls)
	{
		Intent intent = new Intent();
		intent.setClass(context, cls);
		startActivity(intent);
	}

	/** 传递数据的页面跳转 */
	protected void initIntent(Context context, Class<?> cls, String keyString, String valueString)
	{
		Intent intent = new Intent();
		intent.putExtra(keyString, valueString);
		intent.setClass(context, cls);
		startActivity(intent);
	}

	/** 用私钥进行AES加密 */
	public void encryptBase(String inputStr, String privateKey)
	{
		try
		{
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		try
		{
			// 用公钥加密私钥
			encodedData = RSACoder.encryptByPublic(privateKey, C.PUBLIC_KEY);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		// 初始化AES
		AES aes = new AES(privateKey);
		// AES加密
		encriptData = aes.getEncrypt(inputStr);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return super.onTouchEvent(event);
	}
}
